/**
 *It is a demo for practicing JAVA. All codes are opened source by Jian.
 */
package jian.tool.daocode.core;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import jian.tool.daocode.util.JdbcUtil;

/**
 * @author Jian
 * @date 2012-6-24
 * @version
 * 
 */
public class Test {

	public static void main(String[] args) {
		try {
			// showTables(JdbcUtil.getConn());
			// showAllDatabases(JdbcUtil.getConn());

			showTableInfos();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public static void test() {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		try {
			conn = JdbcUtil.getConn();
			String sql = "select * from t_test";
			pstm = conn.prepareStatement(sql);
			rs = pstm.executeQuery();
			pstm.getParameterMetaData();
			while (rs.next()) {
				int id = rs.getInt("id");
				String name = rs.getString("name");
				System.err.printf("【%d,%s】", id, name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstm, conn);
		}
	}

	/**
	 * @param conn
	 *            DataBaseMetaData
	 */
	public static void showTables(Connection conn) {
		DatabaseMetaData metaData = null;
		ResultSet rs = null;
		try {
			metaData = conn.getMetaData();
			rs = metaData.getTables(null, null, null, new String[] { "TABLE" });

			while (rs.next()) {
				System.err.println(rs.getString(1));
				System.err.println(rs.getString(2));
				System.err.println(rs.getString("TABLE_NAME"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void showAllDatabases(Connection conn) {
		DatabaseMetaData metaData = null;
		ResultSet rs = null;
		try {
			metaData = conn.getMetaData();
			rs = metaData.getCatalogs();

			while (rs.next()) {
				System.err.println(rs.getString("TABLE_CAT"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public static void showTableInfos() {
		Connection conn = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;

		@SuppressWarnings("unused")
		ParameterMetaData paramMetaData = null;
		ResultSetMetaData rsMetaData = null;
		try {
			conn = JdbcUtil.getConn("db_battleheart");
			String sql = "select * from t_s_itemtemplate";
			pstm = conn.prepareStatement(sql);
			paramMetaData = pstm.getParameterMetaData();
			rs = pstm.executeQuery();
			rsMetaData = rs.getMetaData();
			pstm.getParameterMetaData();

			int cols = rsMetaData.getColumnCount(); // 查询获取结果的列数
			for (int i = 1; i <= cols; i++) {
				System.out.printf("列 %1$s 的信息\t", i);
				System.out.printf("列名称 %1$s\t", rsMetaData.getColumnName(i));
				System.out.printf("Java类型名称 %1$s\t",
						rsMetaData.getColumnClassName(i));
				System.out.printf("SQL类型名称 %1$s\t",
						rsMetaData.getColumnTypeName(i));
				System.out.printf("显示最大长度 %1$s\t",
						rsMetaData.getColumnDisplaySize(i));
				System.out.println();
			}

			// while (rs.next())
			// {
			// int id = rs.getInt("id");
			// String name = rs.getString("name");
			// System.err.printf("【%d,%s】", id, name);
			// }
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			JdbcUtil.close(rs, pstm, conn);
		}
	}
}
